package com.sromku.simple.fb.example;


import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.hal.model.Activite;
import com.example.hal.model.ActiviteController;
import com.example.hal.model.Evenement;
import com.example.hal.model.EvenementController;
import com.sromku.simple.fb.example.utils.ProfileFacebook;



public class EvenementAddActivity  extends Activity{
	

	 private ImageButton ib;
	 private ImageButton ibh;
	 private Calendar cal;
	 private int day;
	 private int month;
	 private int year;
	 private EditText et;
	 private EditText adressetx;
	 private TextView displayTime;
	    private EditText pickTime;
	 
	    private int pHour;
	    private int pMinute;
	    /** This integer will uniquely define the dialog to be used for displaying time picker.*/
	    static final int DATE_DIALOG_ID = 0;
	    static final int TIME_DIALOG_ID = 1;
	    static final int ADRESSE_DIALOG_ID = 2;

	 private EditText titre;
	 private  EditText texte;
	
	Date dateDebut=new Date();
	
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.add_evenement);

	titre = (EditText) findViewById(R.id.evenementTitre);
	texte = (EditText) findViewById(R.id.evenementTexte);
    ib = (ImageButton) findViewById(R.id.imageButton1);
	  cal = Calendar.getInstance();
	  day = cal.get(Calendar.DAY_OF_MONTH);
	  month = cal.get(Calendar.MONTH);
	  year = cal.get(Calendar.YEAR);
	  et = (EditText) findViewById(R.id.editText);
	  ib.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	            showDialog(DATE_DIALOG_ID);
	        }
	    });
	  et.setText(day + " / " + (month + 1) + " / "
			     + year);
	  et.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	            showDialog(DATE_DIALOG_ID);
	        }
	    });
	Button btn = (Button) findViewById(R.id.addEvenementNow);
	btn.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
			addEvenement();
	    }
	});
	
	
	
	
	   /** Capture our View elements */
    displayTime = (TextView) findViewById(R.id.timeDisplay);

    ibh = (ImageButton) findViewById(R.id.imageButtonHorloge);
    /** Listener for click event of the button */
    ibh.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    /** Get the current time */
    final Calendar cal = Calendar.getInstance();
    pHour = cal.get(Calendar.HOUR_OF_DAY);
    pMinute = cal.get(Calendar.MINUTE);
    
    

    adressetx = (EditText) findViewById(R.id.editTextAdresse);
	 
    adressetx.setOnTouchListener(new View.OnTouchListener() {
	        /*public void onClick(View v) {
	            //showDialog(ADRESSE_DIALOG_ID);

	        }*/

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {

	            Intent intent=new Intent(EvenementAddActivity.this,AdresseGetActivity.class);
			    startActivity(intent);
				// TODO Auto-generated method stub
				return false;
			}
	    });

    /** Display the current time in the TextView */
    updateDisplay();
    
    
    }
	
	
	
	/** Callback received when the user "picks" a time in the dialog */
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                pHour = hourOfDay;
                pMinute = minute;
                updateDisplay();
                displayToast();
            }
        };
     
    /** Updates the time in the TextView */
    private void updateDisplay() {
        displayTime.setText(
            new StringBuilder()
                    .append(pad(pHour)).append(":")
                    .append(pad(pMinute)));
    }
     
    /** Displays a notification when the time is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append(" ").append(displayTime.getText()),   Toast.LENGTH_SHORT).show();
             
    }
     
    /** Add padding to numbers less than ten */
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
     
	
	
	
	

	 @Override
	 @Deprecated
	 protected Dialog onCreateDialog(int id) {
		 switch (id) {
	        case TIME_DIALOG_ID:
	            return new TimePickerDialog(this,
	                    mTimeSetListener, pHour, pMinute, true);
	        case DATE_DIALOG_ID:
	      	  return new DatePickerDialog(this, datePickerListener, year, month, day);
	       // case ADRESSE_DIALOG_ID:

	            //startActivityForResult(intent,SELECT_FILE_REQUEST);
	        	//return 
	      	 
	        
	        }
	        return null;
	 }

	 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
	  public void onDateSet(DatePicker view, int selectedYear,
	    int selectedMonth, int selectedDay) {
	   et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
	     + selectedYear);
	  }
	 };

	

    final void addEvenement()
    {
	final Thread checkUpdate = new Thread() {
	    public void run() {

		
		Evenement u = new Evenement();
		u.setTitre(titre.getText().toString());
		u.setTexte(texte.getText().toString());
		
		

		 u.setUtilisateur_id(Long.valueOf(1));
		 u.setActivite_id(Long.valueOf(1));
		 u.setNbparticipant(Long.valueOf(1));
		 
		 u.setDateDebut(new Date(day,month,day));
		 u.setAdresse("");
		 u.setLongitude(0.1);
		 u.setLatitude(0.1);
		 u.setTheme("");
		 u.setDateCreation(new Date());
		final EvenementController uc = new EvenementController();
		try {
		    uc.create(u);
		} catch (Exception e) {
		    return;
		}
	//	final Intent intent = new Intent(AddEvenementActivity.this,	MainActivity.class);
	//	startActivity(intent);
		 startActivity(new Intent(getApplicationContext(), MainActivity.class));
         finish();
	    }
	};
	checkUpdate.start();
    }

/*
    private void getLatLongFromAddress(String address)
    {
          double lat= 0.0, lng= 0.0;

        Geocoder geoCoder = new Geocoder(this, Locale.getDefault());    
        try 
        {
            List<Address> addresses = geoCoder.getFromLocationName(address , 1);
            if (addresses.size() > 0) 
            {            
                Java.Long.Object.GeoPoint p = new Java.Long.Object.GeoPoint(
                        (int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

                lat=p.getLatitudeE6()/1E6;
                lng=p.getLongitudeE6()/1E6;

                Log.d("Latitude", ""+lat);
            Log.d("Longitude", ""+lng);
            }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
    }*/

}
